package com.propertypoint.service.business;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.propertypoint.presentation.model.Property;
import com.propertypoint.service.general.PropertyFeedProcessStratergy;
import com.propertypoint.util.PropertyFeedUtil;
import com.propertypoint.util.PropertyPointConstants;

public class PropertyGuruFeedStratergy implements PropertyFeedProcessStratergy {

	public static final String D01 = "Boat Quay,Raffles Place,Marina";
	public static final String D02 = "Chinatown,Tanjong Pagar";
	public static final String D03 = "Alexandra,Commonwealth";
	public static final String D04 = "Harbourfront,Telok Blangah";
	public static final String D05 = "Buona Vista,West Coast,Clementi";
	public static final String D06 = "City Hall,Clarke Quay";
	public static final String D07 = "Beach Road,Bugis,Rochor";
	public static final String D08 = "Farrer Park,Serangoon";
	public static final String D09 = "Orchard,River Valley";
	public static final String D10 = "Tanglin Holland";
	public static final String D11 = "Newton,Novena";
	public static final String D12 = "Balestier,Toa Payoh";
	public static final String D13 = "Macpherson,Potong Pasir";
	public static final String D14 = "Eunos,GeyLang,Paya Lebar";
	public static final String D15 = "East Coast,Marine Parade";
	public static final String D16 = "Bedok,Upper East Coast";
	public static final String D17 = "Changi Airport,Changi Village";
	public static final String D18 = "Pasir Ris/Tampines";
	public static final String D19 = "Hougang/Punggol,Sengkang";
	public static final String D20 = "Ang Mo Kio,Bishan,Thomson";
	public static final String D21 = "Clementi Park,Upper Bukit Timah";
	public static final String D22 = "Boon Lay,urong,Tuas";
	public static final String D23 = "Bukit Batok,Bukit Panjang";
	public static final String D24 = "Choa Chu Kang,Tengah";
	public static final String D25 = "Admiralty,Woodlands";
	public static final String D26 = "Mandai,Upper Thomson";
	public static final String D27 = "Sembawang,yishun";
	public static final String D28 = "Seletar,Yio Chu Kang";

	public static final String AU = "Australia";
	public static final String IN = "India";
	public static final String NZ = "New Zealand";
	public static final String FR = "France";
	public static final String BS = "Bahamas";
	public static final String MY = "Malaysia";
	public static final String AE = "Emirates";
	public static final String TH = "THAILAND";
	public static final String GB = "United Kindom";
	public static final String PH = "Philiphines";
	public static final String ID = "Indonesia";
	public static final String JP = "Japan";
	public static final String US = "United States";
	public static final String CY = "Cyprus";
	public static final String ES = "Spain";
	public static final String KH = "Cambodia";
	public static final String CN = "China";
	public static final String CH = "Switzerland";

	public static final String LOCATION = "location";
	public static final String RENT_SALE = "rent_sale";
	public static final String MAX_PRICE = "maxprice";
	public static final String MAX_BED = "maxbed";
	public static final String MIN_BED = "minbed";

	@Override
	public List<Property> processFeed(Map<String, String[]> parameterMap) {
		List<Property> propertyList = new ArrayList<Property>();
		String[] rentSale = parameterMap.get(PropertyPointConstants.RENT_SALE);

		if (!ArrayUtils.isEmpty(rentSale)
				&& !"overseas".equalsIgnoreCase(rentSale[0])) {

			// String[] rentSale =
			// parameterMap.get(PropertyPointConstants.RENT_SALE);
			String[] location = getLocations(parameterMap
					.get(PropertyPointConstants.LOCATION));
			String[] maxBed = parameterMap.get(PropertyPointConstants.BEDROOMS);
			String[] propertytype = parameterMap
					.get(PropertyPointConstants.TYPE);
			String[] maxPrice = getRentMaxPrice(parameterMap
					.get(PropertyPointConstants.MAX_PRICE));

			// CONDO,HDB

			parameterMap = new HashMap<String, String[]>();
			parameterMap.put(LOCATION, location);
			parameterMap.put(RENT_SALE, rentSale);
			parameterMap.put(MAX_PRICE, maxPrice);

			if (maxBed != null && maxBed.length > 0) {
				if (maxBed[0].toUpperCase().contains("STUDIO")) {
					parameterMap.put(MAX_BED, new String[] { "0" });
					parameterMap.put(MIN_BED, new String[] { "0" });
				} else if (maxBed[0].toUpperCase().contains("ANY")) {
					parameterMap.put(MAX_BED, new String[] {});
					parameterMap.put(MIN_BED, new String[] {});
				} else {
					String noOfRooms = maxBed[0].split(" ")[0];
					parameterMap.put(MIN_BED, new String[] { "0" });
					parameterMap.put(MAX_BED, new String[] { noOfRooms });
				}
			}
			// CONDO,APT,WALK,CLUS,EXCON
			// HDB
			// TERRA,DETAC,SEMI,CORN,LBUNG,BUNG,SHOPH,RLAND,TOWN,CON,LCLUS
			if (propertytype != null && propertytype.length > 0) {
				// Apartment / Condo
				if (propertytype[0].toUpperCase().contains("APARTMENT / CONDO")) {
					parameterMap
							.put("property_type_code",
									new String[] { "CONDO"/*
														 * ,"APT","WALK","CLUS",
														 * "EXCON"
														 */});
					parameterMap.put("propertytype", new String[] { "N" });
				}
				// Landed House
				else if (propertytype[0].toUpperCase().contains("LANDED HOUSE")) {
					parameterMap.put("property_type_code",
							new String[] { "TERRA"/*
												 * ,"DETAC","SEMI","CORN","LBUNG"
												 * ,
												 * "BUNG","SHOPH","RLAND","TOWN"
												 * ,"CON","LCLUS"
												 */});
					parameterMap.put("propertytype", new String[] { "L" });
				}
				// HDB
				else if (propertytype[0].toUpperCase().contains("HDB")) {
					parameterMap.put("property_type_code",
							new String[] { "HDB" });
					parameterMap.put("propertytype", new String[] { "H" });
				}
			}

			try {
				JsonObject jsonObject = PropertyFeedUtil.getInstance()
						.callFeed(parameterMap,
								PropertyPointConstants.URL_PROPERTY_GURU);
				JsonElement element = jsonObject.get("value");
				jsonObject = element.getAsJsonObject();
				element = jsonObject.get("items");
				JsonArray arry = element.getAsJsonArray();

				Type listType = new TypeToken<List<Property>>() {
				}.getType();

				propertyList = new Gson().fromJson(arry.toString(), listType);
			} catch (Exception e) {

			}

		}

		else {
			String[] location = getOverSeasLocations(parameterMap
					.get(PropertyPointConstants.LOCATION));
			parameterMap = new HashMap<String, String[]>();
			parameterMap.put("country_code", location);

			try {
				JsonObject jsonObject = PropertyFeedUtil
						.getInstance()
						.callFeed(
								parameterMap,
								PropertyPointConstants.URL_PROPERTY_GURU_OVERSEAS);
				JsonElement element = jsonObject.get("value");
				jsonObject = element.getAsJsonObject();
				element = jsonObject.get("items");
				JsonArray arry = element.getAsJsonArray();

				Type listType = new TypeToken<List<Property>>() {
				}.getType();

				propertyList = new Gson().fromJson(arry.toString(), listType);
			} catch (IOException e) {

				e.printStackTrace();
			}

		}
		return propertyList;
	}

	/*
	 * D01 - Boat Quay,Raffles Place,Marina D02 - Chinatown,Tanjong Pagar D03 -
	 * Alexandra,Commonwealth D04 - Harbourfront,Telok Blangah D05 - Buona
	 * Vista,West Coast,Clementi D06 - City Hall,Clarke Quay D07 - Beach
	 * Road,Bugis,Rochor, D08 - Farrer Park, Serangoon D09 - Orchard,River
	 * Valley D10 - Tanglin Holland D11 - Newton,Novena D12 - Balestier,Toa
	 * Payoh D13 - Macpherson,Potong Pasir D14 - Eunos,GeyLang,Paya Lebar D15 -
	 * East Coast,Marine Parade D16 - Bedok, Upper East Coast D17 - Changi
	 * Airport, Changi Village D18 - Pasir Ris/Tampines D19 -
	 * Hougang/Punggol,Sengkang D20 - Ang Mo Kio,Bishan,Thomson D21 - Clementi
	 * Park,Upper Bukit Timah D22 - Boon Lay, urong,Tuas D23 - Bukit Batok,Bukit
	 * Panjang D24 - Choa Chu Kang,Tengah D25 - Admiralty,Woodlands D26 -
	 * Mandai, Upper THomson D27- Sembawang,yishun D28 - Seletar, Yio Chu Kang
	 */
	private String[] getLocations(String[] locations) {
		String location = null;
		if (locations != null && locations[0] != null) {
			location = locations[0].toUpperCase();
		} else {
			return new String[] {};
		}

		if (D01.toUpperCase().contains(location)) {
			return new String[] { "D01" };
		} else if (D02.toUpperCase().contains(location)) {
			return new String[] { "D02" };
		} else if (D03.toUpperCase().contains(location)) {
			return new String[] { "D03" };
		} else if (D04.toUpperCase().contains(location)) {
			return new String[] { "D04" };
		} else if (D05.toUpperCase().contains(location)) {
			return new String[] { "D05" };
		} else if (D06.toUpperCase().contains(location)) {
			return new String[] { "D06" };
		} else if (D07.toUpperCase().contains(location)) {
			return new String[] { "D07" };
		} else if (D08.toUpperCase().contains(location)) {
			return new String[] { "D08" };
		} else if (D09.toUpperCase().contains(location)) {
			return new String[] { "D09" };
		} else if (D10.toUpperCase().contains(location)) {
			return new String[] { "D10" };
		} else if (D11.toUpperCase().contains(location)) {
			return new String[] { "D11" };
		} else if (D12.toUpperCase().contains(location)) {
			return new String[] { "D12" };
		} else if (D13.toUpperCase().contains(location)) {
			return new String[] { "D13" };
		} else if (D14.toUpperCase().contains(location)) {
			return new String[] { "D14" };
		} else if (D15.toUpperCase().contains(location)) {
			return new String[] { "D15" };
		} else if (D16.toUpperCase().contains(location)) {
			return new String[] { "D16" };
		} else if (D17.toUpperCase().contains(location)) {
			return new String[] { "D17" };
		} else if (D18.toUpperCase().contains(location)) {
			return new String[] { "D18" };
		} else if (D19.toUpperCase().contains(location)) {
			return new String[] { "D19" };
		} else if (D20.toUpperCase().contains(location)) {
			return new String[] { "D20" };
		} else if (D21.toUpperCase().contains(location)) {
			return new String[] { "D21" };
		} else if (D22.toUpperCase().contains(location)) {
			return new String[] { "D22" };
		} else if (D23.toUpperCase().contains(location)) {
			return new String[] { "D23" };
		} else if (D24.toUpperCase().contains(location)) {
			return new String[] { "D24" };
		} else if (D25.toUpperCase().contains(location)) {
			return new String[] { "D25" };
		} else if (D26.toUpperCase().contains(location)) {
			return new String[] { "D26" };
		} else if (D27.toUpperCase().contains(location)) {
			return new String[] { "D27" };
		} else {
			return new String[] { "D28" };
		}

	}

	private String[] getOverSeasLocations(String[] locations) {
		String location = null;
		if (ArrayUtils.isNotEmpty(locations)) {
			location = locations[0].toUpperCase();
		} else {
			return new String[] {};
		}

		if (AU.toUpperCase().contains(location)) {
			return new String[] { "au" };
		} else if (IN.toUpperCase().contains(location)) {
			return new String[] { "in" };
		} else if (NZ.toUpperCase().contains(location)) {
			return new String[] { "nz" };

		} else if (FR.toUpperCase().contains(location)) {
			return new String[] { "fr" };
		} else if (BS.toUpperCase().contains(location)) {
			return new String[] { "bs" };
		} else if (MY.toUpperCase().contains(location)) {
			return new String[] { "my" };
		} else if (AE.toUpperCase().contains(location)) {
			return new String[] { "ae" };
		} else if (TH.toUpperCase().contains(location)) {
			return new String[] { "th" };
		} else if (GB.toUpperCase().contains(location)) {
			return new String[] { "gb" };
		} else if (PH.toUpperCase().contains(location)) {
			return new String[] { "ph" };
		} else if (ID.toUpperCase().contains(location)) {
			return new String[] { "id" };
		} else if (JP.toUpperCase().contains(location)) {
			return new String[] { "jp" };
		} else if (US.toUpperCase().contains(location)) {
			return new String[] { "us" };
		} else if (CY.toUpperCase().contains(location)) {
			return new String[] { "cy" };
		} else if (ES.toUpperCase().contains(location)) {
			return new String[] { "es" };
		} else if (KH.toUpperCase().contains(location)) {
			return new String[] { "kh" };
		} else if (CN.toUpperCase().contains(location)) {
			return new String[] { "cn" };
		} else if (CH.toUpperCase().contains(location)) {
			return new String[] { "ch" };
		} else {
			return new String[] { "" };
		}

	}

	private String[] getRentMaxPrice(String[] maxPrice) {
		String price = null;
		if (maxPrice != null && maxPrice[0] != null) {
			price = maxPrice[0];
			if (price.equalsIgnoreCase(PropertyPointConstants.ANY_PRICE)) {
				return new String[] {};
			}
		} else {
			return new String[] {};
		}
		String value = price.split(" ")[1];
		return new String[] { value };

	}
}
